//
//  ZMLogisticsVC.swift
//  zhonglibang
//
//  Created by Rowling on 2021/1/13.
//

import UIKit

class ZMLogisticsVC: ZMViewController {
    var orderNo = ""
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var expressNum: UILabel!
    
    
    var data : [String : Any]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.zm.register(nibName: "ZMLogisticsCell", reuserId: "ZMLogisticsCell")
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableView.automaticDimension
        title = "查看物流"
        titleLabel.text = ""
        expressNum.text = ""
        imageView.zm.setImage(urlStr: "")
        
        tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
        loadExpress()
    }
    
    func fillData() {
        guard let data = data else {return}
        titleLabel.text = JSON(data)["express"]["expName"].stringValue
        expressNum.text = "单号:" + JSON(data)["express"]["number"].stringValue
        let imgStr = JSON(data)["express"]["logo"].stringValue
        imageView.zm.setImage(urlStr: imgStr)
        tableView.reloadData()
    }
    
    //MARK:--------------------请求--------------------
    func loadExpress() {
        ZMToast.showActivity()
        ZMAPIManage<[String : Any]>.rxSendRequest(CommonAPI.queryExpress(orderNo)).subscribe(onNext:{ [unowned self] resp in
            if resp.code == .success {
                ZMToast.hiddenActivity()
                self.data = resp.data
                self.fillData()
            }
        }).disposed(by: disposeBag)
    }
}

extension ZMLogisticsVC : UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard let data = data else {return 0}
        return JSON(data)["express"]["list"].arrayValue.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ZMLogisticsCell") as! ZMLogisticsCell
        let subData = JSON(data)["express"]["list"].arrayValue[indexPath.row]
        cell.timeLabel.text = subData["time"].stringValue.replacingOccurrences(of: " ", with: "\n")
        cell.contentLabel.text = subData["status"].stringValue
        return cell
    }
}
